package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/setmeal")
public class SetmealController {
    private final SetmealService setmealService;

    //分页查询
    @GetMapping("/page")
    public Result<PageResult> pageQuerySetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("分页查询:{}", setmealPageQueryDTO);
        return Result.success(setmealService.pageQuerySetmeal(setmealPageQueryDTO));
    }

    //新增套餐
    @PostMapping()
    @CacheEvict(value = {"setmeal"},allEntries = true)
    public Result<Setmeal> addSetmeal(@RequestBody SetmealVO setmealVO) {
        log.info("新增套餐:{}", setmealVO);
        setmealService.addSetmeal(setmealVO);
        return Result.success();
    }

    //根据id查询套餐
    @GetMapping("/{id}")
    public Result<SetmealVO> selectSetmealById(@PathVariable Long id) {
        log.info("根据id查询套餐:{}", id);
        return Result.success(setmealService.selectSetmealById(id));
    }

    //套餐起售、停售
    @PostMapping("/status/{status}")
    @CacheEvict(value = {"setmeal"},allEntries = true)
    public Result updateSetmealStatus(@PathVariable Integer status, Integer id) {
        log.info("套餐起售、停售:{},{}",status,id);
        setmealService.updateSetmealStatus(status,id);
        return Result.success();
    }

    //修改套餐
    @PutMapping()
    @CacheEvict(value = {"setmeal_dish"},allEntries = true)
    public Result<Setmeal> updateSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐:{}",setmealDTO);
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }

    //批量删除套餐
    @DeleteMapping()
    @CacheEvict(value = {"setmeal"},allEntries = true)
    public Result<Boolean> deleteSetmeal(@RequestParam List<Long> ids) {
        log.info("批量删除套餐:{}",ids);
        setmealService.deleteSetmeal(ids);
        return Result.success();
    }
}
